import requests
from threading import Thread
from  bs4 import BeautifulSoup

def fetch(url:str):
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
    }
    response = requests.get(url,headers=header)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text,'lxml')
        movies = soup.find_all('div',class_='item')
        movie_data = []

        for movie in movies:
            title = movie.find('span',class_='title').text.strip()
            rating = movie.find('span',class_='rating_num').text.strip()
            movie_data.append({'title':title,'rating':rating})

        for movie in movie_data:
            print(f"电影：{movie['title']},评分：{movie['rating']}")
    else:
        print("请求失败")

def main():
    base_url = 'https://movie.douban.com/top250'
    urls = [f'{base_url}?start={i}' for i in range(0, 250, 25)]

    # base_url = 'https://movie.douban.com/top250'
    # urls = [f'{base_url}?start={i}' for i in range(0, 250, 25)]
    threads = []

    for url in urls:
        thread = Thread(target=fetch,args=(url,))
        threads.append(thread)
        thread.start()
    for thread in threads:
        thread.join()

if __name__=='__main__':
    main()